<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Middleware\AdminMiddleware;
use App\Model\QmGameAccount;
use App\Service\ToolService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: '/admin/Account'), Middleware(AdminMiddleware::class)]
class AccountController extends BaseController {

    /**
     * 获取人员列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $platform = $this->request->query('platform', '');
        $mobile = $this->request->query('mobile', '');
        $account = $this->request->query('account', '');
        $openid = $this->request->query('openid', '');
        $verify_id = $this->request->query('is_verify', '');

        $obj = QmGameAccount::query();
        if (!empty($platform)) {
            $obj = $obj->where('platform', $platform);
        }
        if (!empty($mobile)) {
            $obj = $obj->where('mobile', 'like', "%{$mobile}%");
        }
        if (!empty($account)) {
            $obj = $obj->where('account', 'like', "%{$account}%");
        }
        if (!empty($openid)) {
            $obj = $obj->where('openid', 'like', "%{$openid}%");
        }
        if (isset($verify_id) && $verify_id != '') {
            if ($verify_id == 0) {
                $obj = $obj->where('verify_id', '=', 0);
            } else {
                $obj = $obj->where('verify_id', '!=', 0);
            }
        }
        $listObj = $obj->orderByDesc('id')->paginate((int)$limit);

        return $this->buildSuccess([
            'list'  => $listObj->items(),
            'count' => $listObj->total()
        ]);
    }

    /**
     * 新增
     * @return array
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $platform = $this->request->post('platform', '');
        $openid = $this->request->post('openid', '');
        $mobile = $this->request->post('mobile', '');
        $status = $this->request->post('status', '');
        $account = $this->request->post('account', '');
        $pwd = $this->request->post('password', '');

        if (empty($openid)) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '请传入openid');
        }
        if (empty($mobile)) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '请传入手机号');
        }
        if (empty($platform)) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '请传入平台');
        }
        if (!empty($pwd)) {
            $password = ToolService::userMd5($pwd);
        } else {
            $password = '';
        }

        $model = new QmGameAccount();
        $model->account = $account;
        $model->openid = $openid;
        $model->platform = $platform;
        $model->mobile = $mobile;
        $model->status = $status;
        $model->password = $password;
        $model->save();

        return $this->buildSuccess();
    }

    /**
     * 编辑
     * @return array
     */
    #[PostMapping(path: 'edit')]
    public function edit(): array {
        $id = $this->request->post('id', '');
        $platform = $this->request->post('platform', '');
        $mobile = $this->request->post('mobile', '');
        $pwd = $this->request->post('password', '');
        $status = $this->request->post('status', '');

        if (empty($id) || $id <= 0) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '请传入id');
        }
        if (empty($platform)) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '请传入平台');
        }

        $model = new QmGameAccount();
        $info = $model->where('id', $id)->first();
        if (empty($info)) {
            return $this->buildFailed(ErrorCode::NOT_EXISTS, '数据不存在');
        }
        if (!empty($mobile)) {
            $info->mobile = $mobile;
            $info->platform = $platform;
        }
        if (!empty($status)) {
            $info->status = $status;
        }
        $info->save();

        if (!empty($pwd)) {
            $password = ToolService::userMd5($pwd);
            $model->where('account', $info->account)->update(['password' => $password]);
        }

        return $this->buildSuccess();
    }


}